/**
 * Copyright (c) 2017 ShopJsp. All Rights Reserved.
 * ============================================================================
 * 版权所有 2011 - 今 北京华宇盈通科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得SHOPJSP商业授权之前，您不能将本软件应用于商业用途，否则SHOPJSP将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.shopjsp.com
 * ============================================================================
 * @author 张丁方
 * @CreateTime 2017-3-29 15:20:09
 */
package com.hyyt.shopjsp.product.service.imp;

import com.hyyt.shopjsp.product.dao.ShopProductAttributeDAO;
import com.hyyt.shopjsp.product.pojo.ShopProductAttributeValue;
import com.hyyt.shopjsp.product.pojo.ShopProductAttribute;
import com.hyyt.shopjsp.product.service.IShopProductAttributeValueService;
import com.hyyt.shopjsp.product.service.IShopProductAttributeService;
import com.hyyt.shopjsp.util.common.Utils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Collection;
import org.apache.log4j.Logger;

@Service
public class ShopProductAttributeService implements IShopProductAttributeService {
    private static  final Logger logger = Logger.getLogger(IShopProductAttributeService.class);

    @Resource
    private ShopProductAttributeDAO shopProductAttributeDAO;
    @Resource
    private IShopProductAttributeValueService shopProductAttributeValueService;

    /**
     * 保存SHOP_商品扩展属性
     *
     * @param shopProductAttribute SHOP_商品扩展属性
     * @return 保存后的SHOP_商品扩展属性
     */
    @Override
    public void saveShopProductAttribute(ShopProductAttribute shopProductAttribute) {
        if(Utils.objectIsEmpty(shopProductAttribute.getIsListShow())){
            shopProductAttribute.setIsListShow(0);
        }
        shopProductAttributeDAO.insert(shopProductAttribute);
        List<ShopProductAttributeValue> shopProductAttributeValueList = shopProductAttribute.getShopProductAttributeValueList();
        if(Utils.collectionIsNotEmpty(shopProductAttributeValueList)){
            for(ShopProductAttributeValue shopProductAttributeValue:shopProductAttributeValueList){
                if(Utils.objectIsNotEmpty(shopProductAttributeValue.getAttrValueName()) && Utils.objectIsNotEmpty(shopProductAttributeValue.getSort())) {
                    shopProductAttributeValue.setProductAttrId(shopProductAttribute.getProductAttrId());
                    shopProductAttributeValueService.saveShopAttributeValue(shopProductAttributeValue);
                }
            }
        }
    }

    /**
     * 更新SHOP_商品扩展属性
     *
     * @param shopProductAttribute SHOP_商品扩展属性
     * @return 更新后的SHOP_商品扩展属性
     */
    @Override
    public void updateShopProductAttribute(ShopProductAttribute shopProductAttribute) {
        shopProductAttributeDAO.update(shopProductAttribute);
        List<ShopProductAttributeValue> shopProductAttributeValueList = shopProductAttribute.getShopProductAttributeValueList();
        if(Utils.collectionIsNotEmpty(shopProductAttributeValueList)){
            for(ShopProductAttributeValue shopProductAttributeValue:shopProductAttributeValueList){
                if(Utils.objectIsNotEmpty(shopProductAttributeValue.getSort()) && Utils.objectIsNotEmpty(shopProductAttributeValue.getAttrValueName())){
                    shopProductAttributeValue.setProductAttrId(shopProductAttribute.getProductAttrId());
                    if(Utils.objectIsEmpty(shopProductAttributeValue.getAttrValueId())){
                        shopProductAttributeValueService.saveShopAttributeValue(shopProductAttributeValue);
                    }else {
                        shopProductAttributeValueService.updateShopAttributeValue(shopProductAttributeValue);
                    }
                }
            }
        }
    }

    /**
     * 删除指定ID的SHOP_商品扩展属性
     *
     * @param id SHOP_商品扩展属性ID
     */
    @Override
    public void deleteShopProductAttribute(Integer id) {
        shopProductAttributeDAO.delete(id);
    }

    /**
     * 删除指定ID集合的SHOP_商品扩展属性
     *
     * @param ids SHOP_商品扩展属性ID集合
    */
    public void deleteShopProductAttribute(Collection<Integer> ids){
        shopProductAttributeDAO.batchDelete(ids);
    }

    /**
     * 根据ID加载一个SHOP_商品扩展属性
     *
     * @param id SHOP_商品扩展属性ID
     * @return SHOP_商品扩展属性
     */
    @Override
    public ShopProductAttribute loadShopProductAttribute(Integer id) {
        return shopProductAttributeDAO.load(id);
    }

    /**
    * 查询所有的SHOP_商品扩展属性，通常有最多取30000条记录的限制
    *
    * @return 所有的SHOP_商品扩展属性，通常有最多取30000条记录的限制
    */
    @Override
    public List<ShopProductAttribute> queryAllShopProductAttribute() {
        return shopProductAttributeDAO.queryAll();
    }

    /**
     * 查询SHOP_商品扩展属性
     *
     * @param params 查询参数，为key-value形式
     * @return SHOP_商品扩展属性集合
     */
    @Override
    public List<ShopProductAttribute> queryShopProductAttribute(Map<String, Object> params) {
        return shopProductAttributeDAO.query(params);
    }

    /**
     * 查询SHOP_商品扩展属性
     *
     * @param params   查询参数，没有则传null,分页参数key为“sortColumns”
     * @param startRow 起始行数（不含起始行的数据）
     * @param rowSize  要查询记录数
     * @return SHOP_商品扩展属性集合
     */
    @Override
    public List<ShopProductAttribute> queryShopProductAttribute(Map<String, Object> params, int startRow, int rowSize) {
        return shopProductAttributeDAO.query(params, startRow, rowSize);
    }

    /**
     * 根据商品分类ID，查询SHOP_商品扩展属性
     *
     * @param params   查询参数，没有则传null,分页参数key为“sortColumns”
     * @param startRow 起始行数（不含起始行的数据）
     * @param rowSize  要查询记录数
     * @return SHOP_商品扩展属性集合
     */
    @Override
    public List<Map<String,Object>> queryShopProductAttributeByProductTypeId(Map<String, Object> params, int startRow, int rowSize) {
        return shopProductAttributeDAO.query("queryShopProductAttributeByProductTypeId",params, startRow, rowSize);
    }


    /**
     * 统计SHOP_商品扩展属性
     *
     * @param params 查询参数，没有则传null,分页参数key为“sortColumns”
     * @return 结果集记录数
     */
    @Override
    public int countShopProductAttribute(Map<String, Object> params) {
        return shopProductAttributeDAO.count(params);
    }

    /**
     * 根据商品Id查询商品属性（用于商品详情）
     * @param productId
     * @return
     */
    @Override
    public List<Map<String,Object>> queryProductAttributeList(Integer productId){
        Map<String,Object> params=new HashMap<>();
        params.put("productId",productId);
        return shopProductAttributeDAO.query("queryProductAttributeList",params);
    }
}